package cn.czh.basic.service.impl;

import cn.czh.basic.mapper.BaseMapper;
import cn.czh.basic.query.BaseQuery;
import cn.czh.basic.service.IBaseService;
import cn.czh.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)//查询的事务方式
public class BaseServiceImpl<T> implements IBaseService<T> {
    @Autowired
    private BaseMapper<T> baseMapper;

    /**
     * 查询所有
     * @return
     */
    @Override
    public List<T> findAll() { return baseMapper.loadAll();
    }

    /**
     * 查询单个
     * @param id
     * @return
     */
    @Override
    public T findOne(Long id) {
        return baseMapper.loadById(id);
    }

    /**
     * 添加
     * @param t
     */
    @Override
    public void add(T t) {
        baseMapper.save(t);
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void del(Long id) {
        baseMapper.remove(id);
    }

    /**
     * 修改
     * @param t
     */
    @Override
    public void update(T t) {
        baseMapper.update(t);
    }

    /**
     * 分页查询
     * @param baseQuery
     * @return
     */
    @Override
    public PageList<T> queryPage(BaseQuery baseQuery) {
        //先查询总数量
        Integer count = baseMapper.queryCount(baseQuery);
        if (count == null || count == 0){//如果没有数据
            //就返回一个无参数的PageList集合
            return new PageList<T>();
        }
        //再查询当前页的数据
        List<T> ts = baseMapper.queryData(baseQuery);
        //返回一个有参数的PageList集合
        return new PageList<T>(count,ts);
    }

    /**
     * 批量查询
     * @param ids
     */
    @Override
    public void patchDel(List<Long> ids) {
        baseMapper.patchDel(ids);
    }
}
